package com.example.cofeed.push;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

public class Utils {
    public static final String TAG = "PushDemoActivity";

    public static String logStringCache = "";

    public static void updateContent(String content) {
        Log.d(TAG, "updateContent");
        String logText = "";

        SimpleDateFormat sDateFormat = new SimpleDateFormat("HH-mm-ss", Locale.CHINA);
        logText += sDateFormat.format(new Date()) + ": ";
        logText += content;

        Log.i(TAG, "updateContent : " + logText);

        if (!TextUtils.isEmpty(Utils.logStringCache)) {
            Utils.logStringCache = Utils.logStringCache + "\n";
        }

        Utils.logStringCache = logText + "\n" + Utils.logStringCache;
        String[] logSp = Utils.logStringCache.split("\n");

        if (logSp.length > 50) {
            Utils.logStringCache = Utils.logStringCache.substring(0,
                    Utils.logStringCache.indexOf(logSp[logSp.length - 1]));
        }

//        if (MainActivity.sMainActivity != null) {
//            MainActivity.sMainActivity.updateDisplay();
//        }
    }

    // 获取ApiKey
    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                apiKey = metaData.getString(metaKey);
            }
        } catch (NameNotFoundException e) {

        }
        return apiKey;
    }

    public static ArrayList<String> getTagsList(String originalText) {
        if (originalText == null || originalText.equals("")) {
            return null;
        }
        ArrayList<String> tags = new ArrayList<String>();
        int indexOfComma = originalText.indexOf(',');
        String tag;
        while (indexOfComma != -1) {
            tag = originalText.substring(0, indexOfComma);
            tags.add(tag);

            originalText = originalText.substring(indexOfComma + 1);
            indexOfComma = originalText.indexOf(',');
        }

        tags.add(originalText);
        return tags;
    }

    public static String getLogText(Context context) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getString("log_text", "");
    }

    public static void setLogText(Context context, String text) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        Editor editor = sp.edit();
        editor.putString("log_text", text);
        editor.commit();
    }

    public static String encodeUTF(String source) {

        String code = "";

        try {
            code = URLEncoder.encode(source, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }

        return code;

    }

}
